﻿using App.Model;
using App.Untiys;
using Newtonsoft.Json.Linq;
using SqlSugar;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Win.Dal;

namespace App.Desk
{
    public class Config
    {
        public static SysUser CurrentUser = new SysUser();

        #region 全局变量

        /// <summary>
        /// 公司
        /// </summary>
        public static string Company = ConfigurationManager.AppSettings["Company"].ToString();

        /// <summary>
        /// 链接字符串
        /// </summary>
        public static string ConnString = ConfigurationManager.AppSettings["ConnString"].ToString();

        /// <summary>
        /// 采样机编号
        /// </summary>
        public static string SampingerCode = ConfigurationManager.AppSettings["SampingerCode"].ToString();
        /// <summary>
        /// 采样机名称
        /// </summary>
        public static string SampingerName = ConfigurationManager.AppSettings["SampingerName"].ToString();

        /// <summary>
        /// 采样机IP
        /// </summary>
        public static string SampingerIp = ConfigurationManager.AppSettings["SampingerIp"].ToString();

        /// <summary>
        /// 摄像头IP
        /// </summary>
        public static string IPCameraAddress = ConfigurationManager.AppSettings["IPCameraAddress"].ToString();

        /// <summary>
        /// 摄像头用户
        /// </summary>
        public static string HKWSLogonUser = ConfigurationManager.AppSettings["HKWSLogonUser"].ToString();

        /// <summary>
        /// 摄像头密码
        /// </summary>
        public static string HKWSLogonPass = ConfigurationManager.AppSettings["HKWSLogonPass"].ToString();

        /// <summary>
        /// 三大项api
        /// </summary>
        public static string SdxApiUrl = ConfigurationManager.AppSettings["SdxApiUrl"].ToString();

        /// <summary>
        /// 三大项 采样机 标识
        /// </summary>
        public static string SdxMark = ConfigurationManager.AppSettings["SdxMark"].ToString();

        /// <summary>
        /// 三大项 获取计划接口地址
        /// </summary>
        public static string SdxGetTrainInfo = ConfigurationManager.AppSettings["SdxGetTrainInfo"].ToString();

        /// <summary>
        /// 三大项 上传采样信息接口
        /// </summary>
        public static string SdxUpload = ConfigurationManager.AppSettings["SdxUpload"].ToString();

        /// <summary>
        /// 三大项 服务器时间
        /// </summary>
        public static string SdxGetSystemTime = ConfigurationManager.AppSettings["SdxGetSystemTime"].ToString();
        
        /// <summary>
        /// 三大项 本地服务 获取服务状态
        /// </summary>
        public static string SdxLocalServerGetStatus = ConfigurationManager.AppSettings["SdxLocalServerGetStatus"].ToString();

        /// <summary>
        /// 三大项 本地服务 获取三大项计划
        /// </summary>
        public static string SdxLocalServerGetPlans = ConfigurationManager.AppSettings["SdxLocalServerGetPlans"].ToString();

        /// <summary>
        /// 三大项 本地服务 上传数据
        /// </summary>
        public static string SdxLocalServerUpload = ConfigurationManager.AppSettings["SdxLocalServerUpload"].ToString();

        /// <summary>
        /// 三大项 本地服务 上传几天前到现在的数据
        /// </summary>
        public static int SdxLocalServerUploadDay = ConfigurationManager.AppSettings["SdxLocalServerUploadDay"].ToIntEx();

        /// <summary>
        /// 是否初始化数据库
        /// </summary>
        public static int InitializationDataBase = ConfigurationManager.AppSettings["InitializationDataBase"].ToIntEx();


        #endregion

        public Config()
        {
        }

        /// <summary>
        /// 根据实体生产 GridView列
        /// </summary>
        /// <returns></returns>
        public static List<DataGridViewTextBoxColumn> ModelToDataGridColumns<T>(T t)
        {
            PropertyInfo[] properties = t.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
            List<DataGridViewTextBoxColumn> columns = new List<DataGridViewTextBoxColumn>();
            if (properties != null)
                if (properties.Length > 0)
                    properties.ForEach(res =>
                    {
                        //var Caption = "";
                        // var Visible = true;
                        ((Attribute[])res.GetCustomAttributes()).ForEach(item =>
                        {
                            //if (item is VGridColumn)
                            //{
                            //    Caption = ((VGridColumn)item).HeaderText;
                            //    Visible = ((VGridColumn)item).Visible;
                            //}

                            DataGridViewCellStyle middleCenterCellStyle = new DataGridViewCellStyle();
                            middleCenterCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                            DataGridViewCellStyle detaultCellStyle = new DataGridViewCellStyle();
                            if (item is VGridColumn)
                            {
                                var colimn = ((VGridColumn)item);
                                columns.Add(
                                new DataGridViewTextBoxColumn
                                {
                                    Name = res.Name,
                                    DataPropertyName = res.Name,
                                    HeaderText = colimn.HeaderText,
                                    Visible = colimn.Visible,
                                    MinimumWidth = 55,
                                    Width = colimn.Width,
                                    DisplayIndex = colimn.DisplayIndex,
                                    DefaultCellStyle = colimn.Alignment == "Center" ? middleCenterCellStyle : detaultCellStyle
                                }); ;  
                            }
                        });
                    });
            return columns;
        }

        public static List<string> AleradyUploadUrls(int samplingId)
        {
            List<string> postList = new List<string>();
            int day = Config.SdxLocalServerUploadDay;
            List<ProSampling> data = new List<ProSampling>();
            if (samplingId <= 0)
                data = ProSamplingServices.GetAleradyUploadList(day, samplingId);
            string paraStr = "";
            data.ForEach(res =>
            {
                paraStr = "CarNo$SimplingPoint$SimplingTarget$SamplingPerson$SetBucketWeight$SetBucketNo$SimplingLocation$SimplingStartTime$SimplingEndTime$SupplierCode$BatchInfoId$SimplingCode$TravelNo$EmptyBucketWeight$SimplingPic$SamplingId";
                paraStr = paraStr.Replace("CarNo", res.CarNo);//0
                paraStr = paraStr.Replace("SimplingPoint", res.SimplingPoint.ToStringEx());//1
                paraStr = paraStr.Replace("SimplingTarget", res.VirtuallySimplingLocation.ToStringEx());//2
                paraStr = paraStr.Replace("SamplingPerson", res.SamplingPerson.ToStringEx());//3
                paraStr = paraStr.Replace("SetBucketWeight", res.SimplingWeight.ToStringEx());//4
                paraStr = paraStr.Replace("SetBucketNo", $"{Config.SdxMark}{res.SetBucketNo}");//5
                paraStr = paraStr.Replace("SimplingLocation", res.SimplingLocation);//6
                paraStr = paraStr.Replace("SimplingStartTime", res.SamplingStartTime);//7
                paraStr = paraStr.Replace("SimplingEndTime", res.SamplingEndTime);//8
                paraStr = paraStr.Replace("SupplierCode", res.SupplierCode);//9
                paraStr = paraStr.Replace("BatchInfoId", res.BatchInfoId);//10
                paraStr = paraStr.Replace("SimplingCode", res.SimplingCode);//11
                paraStr = paraStr.Replace("TravelNo", res.TravelNo);//12
                paraStr = paraStr.Replace("EmptyBucketWeight", "0");//13
                paraStr = paraStr.Replace("SimplingPic", res.SimplingPic);//14
                paraStr = paraStr.Replace("SamplingId", res.SamplingId.ToString());//15
                string urlStr = $"{Config.SdxLocalServerUpload}?paraStr={paraStr}";
                postList.Add(urlStr);
            });
            return postList;
        }

        public static List<string> DoUploadDataToSdx(int samplingId)
        {
            List<string> retList = new List<string>();
            var data = AleradyUploadUrls(samplingId);
            data.ForEach(res =>
            {
                var result = DataTranf.GetHTML(res);
                if (result.Contains("OK"))
                {
                    var _rSamplingId = result.Replace("OK", "").ToIntEx();
                    if (ProSamplingServices.SetUploaded(_rSamplingId))
                    {
                        retList.Add($"编号：{_rSamplingId}上传成功");
                    }
                }
                Thread.Sleep(1000 * 5);//*秒传一条
            });
            return retList;
        }
    }
}
